package org.example;

import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class SpireDemo01 {

    public static void main(String[] args) throws Exception {
        //testAddPic();
        //testMergeDoc();
        //testMergeDoc2();

        // 下面这两个效果好
        //testMergeDoc3();
        //mergeWord4();
    }

    /**
     * 测试 Word 中插入一张图片，图片设置为 四周环绕，且占满整个页面
     */
    static void testAddPic(){
        Document doc = new Document();
        Section section = doc.addSection();
        Paragraph para = section.addParagraph();
        //doc.loadFromFile("");

        DocPicture picture = new DocPicture(doc);
        String images = "d:/1.png";
        picture.loadImage(images);

//        section.getPageSetup().getMargins().setTop(0);
//        section.getPageSetup().getMargins().setBottom(0);
//        section.getPageSetup().getMargins().setLeft(0);
//        section.getPageSetup().getMargins().setRight(0);

        float leftMargin = section.getPageSetup().getMargins().getLeft();
        float rightMargin = section.getPageSetup().getMargins().getRight();
        float topMargin = section.getPageSetup().getMargins().getTop();
        float bottomMargin = section.getPageSetup().getMargins().getBottom();

        float clientWidth = section.getPageSetup().getClientWidth();
        float clientHeight = section.getPageSetup().getClientHeight();

        picture.setWidth(clientWidth + leftMargin + rightMargin); //示例：80
        picture.setHeight(clientHeight + topMargin + bottomMargin);//示例：50

        picture.setTextWrappingStyle(TextWrappingStyle.Square);
        //doc.getSections().get(0).getParagraphs().get(1).getChildObjects().insert(0, picture);

        picture.setHorizontalPosition(0F - leftMargin);
        picture.setVerticalPosition(0F - topMargin);

        para.getChildObjects().insert(0, picture);

        doc.saveToFile("d:/封面图片.docx", FileFormat.Docx);
    }


    /**
     * 合并后得样式没了，文章设置得字体样式丢了
     */
    static void testMergeDoc(){
        String filePath1 =  "d:/封面图片.docx";
        String filePath2 = "d:/行业技术报告模板0920.docx";
        Document document = new Document(filePath1);
        document.insertTextFromFile(filePath2, FileFormat.Docx_2013);
        document.saveToFile("d:/学术合并-spire.docx", FileFormat.Docx_2013);
    }

    /**
     * 合并后得 页边距使用得第一个得，导致页边距错误了。
     */
    static void testMergeDoc2() throws Exception {
        String filePath1 =  "d:/封面图片.docx";
        String filePath2 = "d:/行业技术报告模板0920.docx";
        String destDocx = "d:/学术合并-poi.docx";

        NiceXWPFDocument niceXWPFDocument1 = new NiceXWPFDocument(Files.newInputStream(Paths.get(filePath1)));
        NiceXWPFDocument niceXWPFDocument2 = new NiceXWPFDocument(Files.newInputStream(Paths.get(filePath2)));
        NiceXWPFDocument merge = niceXWPFDocument1.merge(niceXWPFDocument2);

        FileOutputStream out = new FileOutputStream(destDocx);
        merge.write(out);
        merge.close();
        out.close();
    }


    /***
     * 效果好
     * 直接将图片插入到第一页得位置，而不是先生成 word，然后再合并两个 word
     */
    static void testMergeDoc3(){
        Document doc = new Document();
        String filePath2 = "d:/行业技术报告模板0920.docx";
        doc.loadFromFile(filePath2);

        DocPicture picture = new DocPicture(doc);
        //String images = "d:/1.png";
        String images = "d:/封面图片.png";
        picture.loadImage(images);

        Section section = doc.getSections().get(0);

        float leftMargin = section.getPageSetup().getMargins().getLeft();
        float rightMargin = section.getPageSetup().getMargins().getRight();
        float topMargin = section.getPageSetup().getMargins().getTop();
        float bottomMargin = section.getPageSetup().getMargins().getBottom();

        float clientWidth = section.getPageSetup().getClientWidth();
        float clientHeight = section.getPageSetup().getClientHeight();

        picture.setWidth(clientWidth + leftMargin + rightMargin); //示例：80
        picture.setHeight(clientHeight + topMargin + bottomMargin);//示例：50

        picture.setTextWrappingStyle(TextWrappingStyle.Square);
        //doc.getSections().get(0).getParagraphs().get(1).getChildObjects().insert(0, picture);

        picture.setHorizontalPosition(0F - leftMargin);
        picture.setVerticalPosition(0F - topMargin);

        Section sectionNew = doc.addSection();
        Paragraph para = sectionNew.addParagraph();

        para.getChildObjects().insert(0, picture);
        doc.getSections().insert(0, sectionNew);

        doc.saveToFile("d:/word插入图片.docx", FileFormat.Docx);
    }

    /**
     * 效果好
     * 读取封面的 word 内容，然后插入到第二个文档中
     */
    static void mergeWord4(){
        Document doc2 = new Document();
        String filePath2 = "d:/行业技术报告模板0920.docx";
        doc2.loadFromFile(filePath2);

        String filePath1 =  "d:/学术快报封面.docx";
        Document doc1 = new Document();
        doc1.loadFromFile(filePath1);
        List<Section> list = new ArrayList<>();

        for(Object item : doc1.getSections()){
            list.add((Section) item);
        }

        Collections.reverse(list);
        for(Section item : list){
            doc2.getSections().insert(0, item.deepClone());
        }

        doc2.saveToFile("d:/word插入word.docx", FileFormat.Docx);

    }

}
